<?php

namespace App\Master\GET;

class Article extends \App\Master\Common {

    /**
     * 详情
     */
    public function view() {
        $id = $this->isG('id', '请提交内容值');
        $base = $this->db('timelog AS tl')->join("{$this->prefix}type AS t ON t.type_id = tl.timelog_type")->where('tl.timelog_id = :timelog_id AND tl.timelog_delete = 0')->find(array('timelog_id' => $id));
        if (empty($base) || $base['timelog_delete'] === '1') {
            $this->error('该时间日志不存在');
        }
        $this->assign($base);
        $this->assign('title', $base['timelog_title']);

        $condition = "timelog_id = :timelog_id";
        $param = array('timelog_id' => $id);

        //内容层数
        $content = $this->db('timelog_content AS tc')->field('tc.*, u.user_id, u.user_name, u.user_head')->join("{$this->prefix}user AS u ON u.user_id =  tc.user_id")->where($condition)->order('tc.timelog_content_createtime ASC')->select($param);
        $this->assign('content', $content);

        //评论
        $comment = $this->db('timelog_comment AS tc')->field('tc.*, u.user_id, u.user_name, u.user_head')->join("{$this->prefix}user AS u ON u.user_id =  tc.user_id")->order('tc.timelog_comment_time ASC')->where($condition)->select($param);
        $this->assign('comment', $comment);

        $type = \Model\Content::listContent([
            'table' => 'type'
        ]);

        $this->assign('type', $type);

        $this->layout();
    }

    /**
     * 查看指定用户的日志
     */
    public function user() {
        $id = $this->isG('id', '请选择您要查看的用户');
        $user = \Model\Content::findContent('user', $id, 'user_id');
        if (empty($user)) {
            $this->error('用户不存在');
        }

        $tmList = \Model\Timelog::tmList($id);
        $this->assign('list', $tmList['list']);
        $this->assign('page', $tmList['page']);
        $this->assign('sidebar', 'Article_user_sidebar');
        $this->assign($user);
        $this->layout();
    }

    /**
     * 发表时间日志
     */
    public function action() {
        if ($this->login === false) {
            $this->jump($this->url('Master-Login-login', ['back_url' => base64_encode($_SERVER['REQUEST_URI'])]));
        }
        $type = \Model\Content::listContent([
            'table' => 'type'
        ]);
        $this->assign('type', $type);
        $this->assign('sidebar', 'Article_action_sidebar');
        $this->layout();
    }

}
